﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;

namespace OrderManagementSystem
{
    public class OrderDetail
    {
        public string ProductName { get; set; }
        public double Price { get; set; }
        public int Quantity { get; set; }

        public override string ToString()
        {
            return $"Product: {ProductName}, Price: {Price}, Quantity: {Quantity}";
        }
    }

     public class Order
    {
        public int OrderId { get; set; }
        public string Customer { get; set; }
        public List<OrderDetail> Details { get; set; }
        public double TotalAmount => Details.Sum(detail => detail.Price * detail.Quantity);

        public override string ToString()
        {
            return $"Order ID: {OrderId}, Customer: {Customer}, Total Amount: {TotalAmount}";
        }

        public override bool Equals(object obj)
        {
            if (obj is Order other)
            {
                return this.OrderId == other.OrderId;
            }
            return false;
        }
    }

    class OrderService
    {
        private List<Order> orders = new List<Order>();

        public void AddOrder(Order order)
        {
            if (!orders.Contains(order))
            {
                orders.Add(order);
            }
            else
            {
                throw new ArgumentException("Order already exists.");
            }
        }

        public void RemoveOrder(int orderId)
        {
            Order order = orders.FirstOrDefault(o => o.OrderId == orderId);
            if (order != null)
            {
                orders.Remove(order);
            }
            else
            {
                Console.WriteLine("Order not found.");
            }
        }

        public void ModifyOrder(int orderId, Order newOrder)
        {
            Order order = orders.FirstOrDefault(o => o.OrderId == orderId);
            if (order != null)
            {
                orders.Remove(order);
                orders.Add(newOrder);
            }
            else
            {
                Console.WriteLine("Order not found.");
            }
        }

        public List<Order> QueryOrders(Func<Order, bool> condition)
        {
            return orders.Where(condition).OrderBy(o => o.TotalAmount).ToList();
        }

        public void SortOrders()
        {
            orders.Sort((x, y) => x.OrderId.CompareTo(y.OrderId));
        }

        public void SortOrders(Func<Order, object> keySelector)
        {
            orders = orders.OrderBy(keySelector).ToList();
        }

        public void Export(string filePath)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
            using (TextWriter writer = new StreamWriter(filePath))
            {
                serializer.Serialize(writer, orders);
            }
            Console.WriteLine("Orders exported successfully.");
        }

        public void Import(string filePath)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
            using (TextReader reader = new StreamReader(filePath))
            {
                List<Order> importedOrders = (List<Order>)serializer.Deserialize(reader);
                orders.AddRange(importedOrders);
            }
            Console.WriteLine("Orders imported successfully.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            OrderService orderService = new OrderService();

            // 添加订单
            Order order1 = new Order { OrderId = 1, Customer = "Alice", Details = new List<OrderDetail> { new OrderDetail { ProductName = "Product1", Price = 10, Quantity = 2 } } };
            orderService.AddOrder(order1);

            Order order2 = new Order { OrderId = 2, Customer = "Bob", Details = new List<OrderDetail> { new OrderDetail { ProductName = "Product2", Price = 20, Quantity = 1 } } };
            orderService.AddOrder(order2);

            // 查询订单
            List<Order> result = orderService.QueryOrders(o => o.Customer == "Alice");
            foreach (var order in result)
            {
                Console.WriteLine(order);
            }

            // 修改订单
            Order newOrder = new Order { OrderId = 1, Customer = "Alice", Details = new List<OrderDetail> { new OrderDetail { ProductName = "Product3", Price = 30, Quantity = 3 } } };
            orderService.ModifyOrder(1, newOrder);

            // 删除订单
            orderService.RemoveOrder(2);

            // 排序订单
            orderService.SortOrders();

            // 显示所有订单
            foreach (var order in orderService.QueryOrders(o => true))
            {
                Console.WriteLine(order);
            }

            // 导出订单
            orderService.Export("orders.xml");

            // 清空订单列表
            orderService = new OrderService();

            // 导入订单
            orderService.Import("orders.xml");

            // 显示导入的订单
            foreach (var order in orderService.QueryOrders(o => true))
            {
                Console.WriteLine(order);

            }
            Console.ReadLine();
        }
    }
}
